// UnitInfoController.java
package com.wormhole.wormholemanagement.controller;

import com.wormhole.wormholemanagement.entity.UnitInfo;
import com.wormhole.wormholemanagement.service.UnitInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * UnitInfo实体的REST控制器
 * 提供对单位信息的增删改查RESTful API接口
 */
@RestController
@RequestMapping("/api/unitinfo")
@Tag(name = "单位信息管理", description = "提供单位信息的增删改查API")
public class UnitInfoController {

    @Autowired
    private UnitInfoService unitInfoService;

    /**
     * 获取所有单位信息列表
     *
     * @return 包含所有单位信息的列表
     */
    @GetMapping
    @Operation(summary = "获取所有单位信息", description = "返回系统中所有单位信息的列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功获取单位信息列表")
    })
    public List<UnitInfo> getAllUnitInfos() {
        return unitInfoService.findAll();
    }

    /**
     * 根据ID获取特定单位信息
     *
     * @param id 单位信息的主键ID
     * @return ResponseEntity包含单位信息对象或错误状态
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取单位信息", description = "根据指定的ID返回对应的单位信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功获取单位信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = UnitInfo.class))),
            @ApiResponse(responseCode = "404", description = "未找到指定ID的单位信息")
    })
    public ResponseEntity<UnitInfo> getUnitInfoById(
            @Parameter(description = "单位信息ID", required = true, example = "1")
            @PathVariable Long id) {
        Optional<UnitInfo> unitInfo = unitInfoService.findById(id);
        if (unitInfo.isPresent()) {
            return new ResponseEntity<>(unitInfo.get(), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    /**
     * 创建新的单位信息
     *
     * @param unitInfo 包含单位信息数据的请求体
     * @return 保存后的单位信息对象
     */
    @PostMapping
    @Operation(summary = "创建新的单位信息", description = "创建一个新的单位信息并保存到数据库")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功创建单位信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = UnitInfo.class))),
            @ApiResponse(responseCode = "400", description = "请求参数错误")
    })
    public UnitInfo createUnitInfo(
            @Parameter(description = "单位信息对象", required = true)
            @RequestBody UnitInfo unitInfo) {
        return unitInfoService.save(unitInfo);
    }

    /**
     * 更新现有的单位信息
     *
     * @param id 单位信息的主键ID
     * @param unitInfoDetails 包含更新数据的请求体
     * @return ResponseEntity包含更新后的单位信息对象或错误状态
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新单位信息", description = "根据指定ID更新对应的单位信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功更新单位信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = UnitInfo.class))),
            @ApiResponse(responseCode = "404", description = "未找到指定ID的单位信息"),
            @ApiResponse(responseCode = "400", description = "请求参数错误")
    })
    public ResponseEntity<UnitInfo> updateUnitInfo(
            @Parameter(description = "单位信息ID", required = true, example = "1")
            @PathVariable Long id,
            @Parameter(description = "更新的单位信息对象", required = true)
            @RequestBody UnitInfo unitInfoDetails) {
        Optional<UnitInfo> optionalUnitInfo = unitInfoService.findById(id);

        if (optionalUnitInfo.isPresent()) {
            UnitInfo unitInfo = optionalUnitInfo.get();
            // 更新单位信息的各个字段
            unitInfo.setUnit_name(unitInfoDetails.getUnit_name());
            unitInfo.setRecordGroupNum(unitInfoDetails.getRecordGroupNum());
            unitInfo.setFileType(unitInfoDetails.getFileType());
            unitInfo.setRetentionPeriod(unitInfoDetails.getRetentionPeriod());
            unitInfo.setOrgIssue(unitInfoDetails.getOrgIssue());
            unitInfo.setCaseFileCate(unitInfoDetails.getCaseFileCate());
            unitInfo.setCreateUser(unitInfoDetails.getCreateUser());
            unitInfo.setUpdateUser(unitInfoDetails.getUpdateUser());

            UnitInfo updatedUnitInfo = unitInfoService.save(unitInfo);
            return new ResponseEntity<>(updatedUnitInfo, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    /**
     * 根据ID删除单位信息
     *
     * @param id 要删除的单位信息的主键ID
     * @return ResponseEntity表示操作结果状态
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除单位信息", description = "根据指定ID删除对应的单位信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "204", description = "成功删除单位信息"),
            @ApiResponse(responseCode = "404", description = "未找到指定ID的单位信息"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public ResponseEntity<HttpStatus> deleteUnitInfo(
            @Parameter(description = "要删除的单位信息ID", required = true, example = "1")
            @PathVariable Long id) {
        try {
            unitInfoService.deleteById(id);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
